<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
	<xsl:param name="q"/>
	<xsl:param name="start"/>
	<xsl:param name="category"/>
	<xsl:param name="sort"/>
	<xsl:param name="offset" as="xs:integer"/>
	<xsl:param name="limit" as="xs:integer"/>
	<xsl:param name="solr-url"/>
	<xsl:param name="tokenized_q" select="tokenize($q, ' AND ')"/>

	<xsl:template match="/">
		<xsl:apply-templates select="descendant::lst[@name='facet_fields']/lst[@name=$category]"/>
	</xsl:template>

	<xsl:template match="lst[@name=$category]">
		<xsl:variable name="facet" select="@name"/>
		<xsl:variable name="name">
			<xsl:choose>
				<xsl:when test="@name = 'city_facet'">City</xsl:when>
				<xsl:when test="@name = 'collection_facet'">Collection</xsl:when>
				<xsl:when test="@name = 'corpname_facet'">Political Entity</xsl:when>
				<xsl:when test="@name = 'deity_facet'">Deity</xsl:when>
				<xsl:when test="@name = 'denomination_facet'">Denomination</xsl:when>
				<xsl:when test="@name = 'department_facet'">Department</xsl:when>
				<xsl:when test="@name = 'famname_facet'">Dynasty</xsl:when>
				<xsl:when test="@name = 'format_facet'">Format</xsl:when>
				<xsl:when test="@name = 'institution_facet'">Institution</xsl:when>
				<xsl:when test="@name = 'persname_facet'">Name</xsl:when>
				<xsl:when test="@name = 'material_facet'">Material</xsl:when>
				<xsl:when test="@name = 'region_facet'">Region</xsl:when>
				<xsl:when test="@name = 'state_facet'">State</xsl:when>
				<xsl:when test="@name = 'script_facet'">Script</xsl:when>
				<xsl:when test="@name = 'subject_facet'">Subject</xsl:when>
				<xsl:when test="@name = 'era_sint'">Century</xsl:when>
			</xsl:choose>
		</xsl:variable>
		<xsl:if test="count(int) &gt; 1">
			<li style="display:table; width:100%;background:none; padding:0;">
				<xsl:choose>
					<xsl:when test="$sort = 'count'">
						<span class="sort_facet" id="{$category}-index" label="{$q}">Sort
							alphabetically</span>
					</xsl:when>
					<xsl:when test="$sort = 'index'">
						<span class="sort_facet" id="{$category}-count" label="{$q}">Sort by
							occurrences</span>
					</xsl:when>
				</xsl:choose>
				<span style="float:right" class="close_facets" id="{$category}-close">close</span>
			</li>			
		</xsl:if>
		<xsl:call-template name="paging"/>
		<xsl:for-each select="int">
			<xsl:variable name="matching_term">
				<xsl:value-of select="concat($facet, ':&#x022;', @name, '&#x022;')"/>
			</xsl:variable>


			<li class="term">
				<xsl:choose>
					<xsl:when test="contains($q, $matching_term)">
						<xsl:variable name="new_query">
							<xsl:for-each select="$tokenized_q[not($matching_term = .)]">
								<xsl:value-of select="."/>
								<xsl:if test="position() != last()">
									<xsl:text> AND </xsl:text>
								</xsl:if>
							</xsl:for-each>
						</xsl:variable>

						<xsl:choose>
							<xsl:when test="$name = 'Century'">
								<div class="fn">
									<xsl:call-template name="regularize_century">
										<xsl:with-param name="term" select="@name"/>
									</xsl:call-template>
								</div>
							</xsl:when>
							<xsl:otherwise>
								<div class="fn">
									<xsl:value-of select="@name"/>
								</div>
							</xsl:otherwise>
						</xsl:choose>

						<div class="ft">
							<xsl:text> [</xsl:text>
							<a
								href="?q={if (string($new_query)) then encode-for-uri($new_query) else '*:*'}"
								>X</a>
							<xsl:text>]</xsl:text>
						</div>
					</xsl:when>
					<xsl:otherwise>
						<xsl:choose>
							<xsl:when test="$name = 'Century'">
								<div class="fn">
									<xsl:choose>
										<xsl:when test="$q = '*:*'">
											<a href="?q={$facet}:&#x022;{@name}&#x022;">
												<xsl:call-template name="regularize_century">
												<xsl:with-param name="term" select="@name"/>
												</xsl:call-template>
											</a>
										</xsl:when>
										<xsl:otherwise>
											<a
												href="?q={$q} AND {$facet}:&#x022;{@name}&#x022;">
												<xsl:call-template name="regularize_century">
												<xsl:with-param name="term" select="@name"/>
												</xsl:call-template>
											</a>
										</xsl:otherwise>
									</xsl:choose>

								</div>
							</xsl:when>
							<xsl:otherwise>
								<div class="fn">
									<xsl:choose>
										<xsl:when test="$q = '*:*'">
											<a href="?q={$facet}:&#x022;{@name}&#x022;">
												<xsl:value-of select="@name"/>
											</a>
										</xsl:when>
										<xsl:otherwise>
											<a
												href="?q={$q} AND {$facet}:&#x022;{@name}&#x022;">
												<xsl:value-of select="@name"/>
											</a>
										</xsl:otherwise>
									</xsl:choose>
								</div>
							</xsl:otherwise>
						</xsl:choose>
						<div class="ft">
							<xsl:value-of select="."/>
						</div>
					</xsl:otherwise>
				</xsl:choose>
			</li>
		</xsl:for-each>
		<xsl:call-template name="paging"/>
	</xsl:template>
	
	<xsl:template name="paging">
		<xsl:variable name="next">
			<xsl:value-of
				select="count(document(concat($solr-url, 'select?q=', encode-for-uri($q), '&amp;category=', $category, '&amp;facet.field=', $category, '&amp;facet.sort=', $sort, '&amp;facet.limit=', $limit, '&amp;f.', $category, '.facet.offset=', $offset + $limit, '&amp;rows=', 0))//lst[@name='facet_fields']/lst[@name=$category]/int)"
			/>
		</xsl:variable>
		<li style="text-align:right;background:none;padding:0">
			<span style="float:left;">
				<xsl:text>Page: </xsl:text>
				<xsl:value-of select="($offset div $limit) + 1"/>
			</span>
			<xsl:choose>
				<xsl:when test="$offset = 0">
					<span>Previous</span>
				</xsl:when>
				<xsl:otherwise>
					<span id="{$category}-prev" class="page-facets" label="{$q}"
						title="{$offset - $limit}:{$sort}">Previous</span>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:text> | </xsl:text>
			<xsl:choose>
				<xsl:when test="number($next)= 0">
					<span>Next</span>
				</xsl:when>
				<xsl:otherwise>
					<span id="{$category}-next" class="page-facets" label="{$q}"
						title="{$offset + $limit}:{$sort}">Next</span>
				</xsl:otherwise>
			</xsl:choose>
		</li>
	</xsl:template>

	<xsl:template name="regularize_century">
		<xsl:param name="term"/>
		<xsl:choose>
			<xsl:when test="$term = '-5'">
				<xsl:text>Fifth Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '-4'">
				<xsl:text>Fourth Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '-3'">
				<xsl:text>Third Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '-2'">
				<xsl:text>Second Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '-1'">
				<xsl:text>First Century B.C.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '1'">
				<xsl:text>First Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '2'">
				<xsl:text>Second Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '3'">
				<xsl:text>Third Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '4'">
				<xsl:text>Fourth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '5'">
				<xsl:text>Fifth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '6'">
				<xsl:text>Sixth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '7'">
				<xsl:text>Seventh Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '8'">
				<xsl:text>Eight Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '9'">
				<xsl:text>Nineth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '10'">
				<xsl:text>Tenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '11'">
				<xsl:text>Eleventh Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '12'">
				<xsl:text>Twelfth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '13'">
				<xsl:text>Thirteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '14'">
				<xsl:text>Fourteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '15'">
				<xsl:text>Fifteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '16'">
				<xsl:text>Sixteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '17'">
				<xsl:text>Seventeenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '18'">
				<xsl:text>Eighteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '19'">
				<xsl:text>Nineteenth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '20'">
				<xsl:text>Twentieth Century A.D.</xsl:text>
			</xsl:when>
			<xsl:when test="$term = '21'">
				<xsl:text>Twenty-First Century A.D.</xsl:text>
			</xsl:when>
			<xsl:otherwise>other</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
